package game.io;

import game.Game;
import game.classes.GameMode;
import game.sound.AudioPlayer;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JSlider;

/**
 * @author Chris
 */
public final class PlayerScreen extends JPanel {

    public static final int TOP_PALET = 1;
    public static final int CENTER_PALET = 2;
    public static final int BOTTOM_PALET = 3;
    private BufferedImage buffer_top;
    private BufferedImage buffer_center;
    private BufferedImage buffer_bottom;
    private GameMode mode;

    public PlayerScreen(GameMode gamemode) {
        setLayout(null);
        setDoubleBuffered(true);
        setBackground(Color.DARK_GRAY);

        buffer_top = new BufferedImage(570, 100, BufferedImage.TYPE_INT_RGB);
        buffer_center = new BufferedImage(Game.BWIDTH, Game.BHEIGHT, BufferedImage.TYPE_INT_RGB);
        buffer_bottom = new BufferedImage(Game.BWIDTH, 50, BufferedImage.TYPE_INT_RGB);



        this.mode = gamemode;

        JButton pause = new JButton("Pausa");
        pause.setBounds(580, 35, 100, 30);
        pause.setFocusable(false);
        pause.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        mode.notifyPause();
                    }
                });
        add(pause);

        JSlider volume = new JSlider(JSlider.VERTICAL, 0, 100, 80);
        volume.addChangeListener(new VolumeSliderChange());
        volume.setBackground(Color.DARK_GRAY);
        volume.setBounds(710, 5, 50, 90);
        volume.setFocusable(false);
        add(volume);

        cleanBuffers();
    }

    public Graphics getCanvas(int p) {
        switch (p) {
            case TOP_PALET:
                return buffer_top.getGraphics();
            case CENTER_PALET:
                return buffer_center.getGraphics();
            case BOTTOM_PALET:
                return buffer_bottom.getGraphics();
            default:
                throw new UnsupportedOperationException("WRONG PALET");
        }
    }

    public void flushBuffers() {
        Graphics g = getGraphics();
        if (g != null) {
            g.drawImage(buffer_top, 0, 0, this);
            g.drawImage(buffer_center, 0, 100, this);
            g.drawImage(buffer_bottom, 0, 550, this);
            g.dispose();
        }

        cleanBuffers();
    }

    public void prepare() {
        super.paint(getGraphics());
    }

    public void pauseMode() {
        String opts[] = {"Continuar jugando", "Abandonar partida", "Tomar un cafe"};

        int action = JOptionPane.showOptionDialog(this,
                "Que deseas hacer?",
                "Juego en pausa",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null, opts, opts[0]);
        if (action == 1) {
            mode.notifyExit();
        } else if (action == 2) {
            JOptionPane.showMessageDialog(this, "No implementado en la versión beta");
            pauseMode();
        } else {
            mode.notifyPause();
        }
    }

    public void deathMode() {
        JOptionPane.showMessageDialog(this, "Perdiste");
        mode.notifyExit();
    }

    private void cleanBuffers() {
        Graphics g;

        g = buffer_top.getGraphics();
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, buffer_top.getWidth(), buffer_top.getHeight());
        g.dispose();

        g = buffer_center.getGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, buffer_center.getWidth(), buffer_center.getHeight());
        g.dispose();

        g = buffer_bottom.getGraphics();
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, buffer_bottom.getWidth(), buffer_bottom.getHeight());
        g.dispose();
    }

    private class VolumeSliderChange implements javax.swing.event.ChangeListener {

        public void stateChanged(javax.swing.event.ChangeEvent e) {
            JSlider slider = (JSlider) e.getSource();
            AudioPlayer.changeGain(slider.getValue());
        }
    }
}
